package com.alpaak.common.utils;

import com.alpaak.common.utils.log.Log4jUtil;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.rpc.ParameterMode;
import java.util.Map;

/**
 * 调用webservice(xml)工具类
 * 
 */

public class WebServiceUtil {

	/*
	 * 调用webservice接口的工具类
	 */
	public static Object webServiceConn(Object baseReq,Object baseResponse) {
		return webServiceConn(baseReq, baseResponse, null);
	}
	
	public static Object webServiceConn(Object baseReq,Object baseResponse,Map<String,Object> conf) {

		String endPoint = "http://192.6.84.214:82/services/HIIPService"; // 接口地址
																		// （提交数据的接口地址，注意去掉？wsdl）
		String namespace = "http://www.winning.com.cn"; // 命名空间地址（上传接口方法所在命名空间）

		String tranCode = "";
		String PatientClass = "";
		if(conf!=null){
			if(conf.containsKey("endPoint")){
				endPoint = (String)conf.get("endPoint");
			}
			if(conf.containsKey("namespace")){
				namespace = (String)conf.get("namespace");
			}
			if(conf.containsKey("tranCode")){
				tranCode = (String)conf.get("tranCode");
			}
			if(conf.containsKey("PatientClass")){
				PatientClass = (String)conf.get("PatientClass");
			}
		}
		
		//将请求参数对象转成xml
		String reqXML = JaxbXmlUtil.convertToXml(baseReq);
		
		//调用webService方法
		String response = null;
		String filePath = "";
		String url = "C:\\Users\\asus\\Desktop\\医院接口返回数据\\";
		
		if("MDT02".equals(tranCode)){
			filePath = url+"MDT02.txt";
		}else if("MDT03".equals(tranCode)){
			filePath = url+"MDT03.txt";
		}else if("MDT01".equals(tranCode)){
			filePath = url+"MDT01.txt";
		}else if("MDT04".equals(tranCode)){
			filePath = url+"MDT04.txt";
		}else if("PAT03".equals(tranCode)){
			filePath = url+"PAT03.txt";
		}else if("PAT01".equals(tranCode)&&"1".equals(PatientClass)){
			filePath = url+"PAT01-门诊.txt";
		}else if("PAT01".equals(tranCode)&&"2".equals(PatientClass)){
			filePath = url+"PAT01-住院.txt";
		}else if("PAT05".equals(tranCode)&&"1".equals(PatientClass)){
			filePath = url+"PAT05-住院.txt";
		}else if("PAT05".equals(tranCode)&&"2".equals(PatientClass)){
			filePath = url+"PAT05-门诊.txt";
		}else if("PAT04".equals(tranCode)){
			filePath = url+"PAT04.txt";
		}else if("RPT01".equals(tranCode)){
			filePath = url+"RPT01.txt";
		}
		
		
		Log4jUtil.debug(reqXML);
		//实际使用
		
		response = WebServiceUtil.invokRemoveWSDLByAxis(reqXML, endPoint, namespace, "getData");
		
//		String log = StringUtil.subString2j(response, "<ERROR>", "</ERROR>");
//		if(!"-1".equals(log)){
//			Log4jUtil.debug("错误信息："+log);
//		}
		
		//本地测试
//		response = (FileUtil.readTextFile(filePath, "GBK")).trim();
		Log4jUtil.debug(response);
		
		if(response.indexOf("<Body/>")!=-1){
			return null;
		}
		
		baseResponse = JaxbXmlUtil.convertToJavaBean(response.trim(), baseResponse.getClass());
		
		return baseResponse;
	}
	
	/**
	 * axis方式访问webservice
	 * 
	 * @param reqXML
	 *            请求的xml的报文
	 * @param endPoint
	 *            webservice的wsdl地址,例如：http://1.202.235.66:7002/webService/
	 *            PartsService?wsdl
	 * @param namespace
	 *            调用方法的命名空间
	 * @param method
	 *            调用的方法名
	 * @return 返回调用的报文(xml)
	 * 
	 */
	public static String invokRemoveWSDLByAxis(String reqXML, String endPoint, String namespace, String method) {
		String responseXML = "";
		Service service = null;
		try {
			service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(endPoint);
			//设置soap协议，默认使用soap11！！！
			call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP12_CONSTANTS);
			call.setOperationName(method);
			call.setOperationName(new javax.xml.namespace.QName(namespace, method));
			call.addParameter("inxml", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);
			responseXML = (String) call.invoke(new Object[] { reqXML });
		} catch (Throwable e) {
			Log4jUtil.error(e.getMessage(), e);
		}
		return responseXML.trim();
	}
	
	/**
	 * 封装的访问webservice的工具类:（有局限性，数据量小的xml报文可用，较大则会出现stackoverflow的异常）
	 * 
	 * @param reqXML
	 *            请求的xml
	 * @param endPoint
	 *            webservice的路径除去?wsdl:例如：http://1.202.235.66:7002/webService/
	 *            PartsService（没有?wsdl）
	 * @param namespace
	 *            名称空间
	 * @param method
	 *            方法名称
	 * @return 返回报文结果(xml)
	 */
	// public static String loadWebService(String reqXML, String endPoint,
	// String namespace, String method) {
	// String responseXml = "";
	// RPCServiceClient serviceClient = null;
	// try {
	// /* 调用理赔系统的接口 */
	// String nameSpace = namespace;
	// QName qname = new QName(nameSpace, method);
	// serviceClient = new RPCServiceClient();
	// Options options = serviceClient.getOptions();
	// EndpointReference targetEPR = new EndpointReference(endPoint);
	// options.setTo(targetEPR);
	// options.setTimeOutInMilliSeconds(30000);
	// Object[] rtnObjs = serviceClient.invokeBlocking(qname, new Object[] {
	// reqXML },
	// new Class[] { String.class });
	// responseXml = rtnObjs[0].toString();
	// } catch (Exception e) {
	// e.toString();
	// }
	// return responseXml;
	// }
	//
	

}
